package com.dc.listener;

import com.dc.util.CountUtil;
import lombok.extern.slf4j.Slf4j;
import org.testng.*;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author xcj
 * @date 2022/5/30 10:54
 */
@Slf4j
public class MyTestNGListener implements IExecutionListener {

    static Date date = new Date();
    static String form = String.format("%tF", date);
    static String hour = String.format("%tH", date);
    static String minute = String.format("%tM", date);
    static String second = String.format("%tS", date);

    /**
     * TestNG重跑机制，当所有用例跑完后，重跑失败的testng-failer.xml文件
     */
    public void onExecutionFinish() {
        //自定义一个CountUtil类，单例模式，用于计算执行次数
        int count = CountUtil.getInstance().getCount();

        String indexPath = "./test-output/index.html";
        String newIndexPath = "./test-output/all_index_" + form + hour + minute + second + ".html";
        String restartIndexPath = "./test-output/index_restart_" + form + hour + minute + second + ".html";
        String failedPath = "./test-output/testng-failed.xml";
        String newFailedPath = "./test-output/all_testng-failed_" + form + hour + minute + second + ".xml";
        String restartFailedPath = "./test-output/testng-failed_restart_" + form + hour + minute + second + ".xml";

        File indeFile = new File(indexPath);
        File failedFile = new File(failedPath);

        //count设置重跑次数
        if (count == 1 && failedFile.exists() && indeFile.exists()) {
            log.info("testng-failed.xml文件已存在,开始重跑失败用例！");

            //重命名FailedReport生成的xml，避免后续重跑导致文件被覆盖
            File newFile = new File(newFailedPath);
            //重命名ExtendReport生成的测试报告，避免后续重跑导致文件被覆盖
            File newIndexFile = new File(newIndexPath);
            //StandardCopyOption.REPLACE_EXISTING 表示存在就替换
            try {
                Files.move(indeFile.toPath(), newIndexFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
                Files.move(failedFile.toPath(), newFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
            } catch (IOException e) {
                e.printStackTrace();
            }

            TestNG testNG = new TestNG();
            testNG.setUseDefaultListeners(false);
            testNG.addListener(new MyFailedReporter());
            testNG.addListener(new ExtentTestNGIReporterListener());
            testNG.addListener(new MyTestNGListener());
            List list = new ArrayList();
            list.add(newFailedPath);
            testNG.setTestSuites(list);
            testNG.run();

        } else {
            if (failedFile.exists()) {
                log.info("IExecutionListener onExecutionFinish!   重跑测试用例也失败了！");

                //重命名重跑的测试报告和错误xml
                File restartIndexFile = new File(restartIndexPath);
                File restartFailedFile = new File(restartFailedPath);
                try {
                    Files.move(indeFile.toPath(), restartIndexFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
                    Files.move(failedFile.toPath(), restartFailedFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } else {
                log.info("程序结束！");
            }
        }
    }
}